package com.bs.modules.use.service.impl;

import cn.hutool.core.date.DateUtil;
import com.bs.common.utils.DateUtils;
import com.bs.modules.app.service.UserService;
import com.bs.modules.use.entity.AppPriceEntity;
import com.bs.modules.use.entity.AppUserEntity;
import com.bs.modules.use.service.AppPriceService;
import com.bs.modules.use.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bs.common.utils.PageUtils;
import com.bs.common.utils.Query;

import com.bs.modules.use.dao.AppPowerDao;
import com.bs.modules.use.entity.AppPowerEntity;
import com.bs.modules.use.service.AppPowerService;


@Service("appPowerService")
public class AppPowerServiceImpl extends ServiceImpl<AppPowerDao, AppPowerEntity> implements AppPowerService {

  @Autowired
  private AppPriceService appPriceService;

  @Autowired
  private AppUserService appUserService;

  @Autowired
  private AppPowerDao appPowerDao;


  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<AppPowerEntity> page = this.page(
      new Query<AppPowerEntity>().getPage(params),
      new QueryWrapper<AppPowerEntity>()
    );

    for (AppPowerEntity record : page.getRecords()) {
      AppPriceEntity appPriceEntity = appPriceService.getById(record.getPowerPriceid());
      record.setPowerPriceName(appPriceEntity.getPowerprice());

      AppUserEntity appUserEntity = appUserService.getById(record.getUserId());
      record.setUserName(appUserEntity.getUserUsername());

      record.setPowerStatusCn("1".equals(record.getPowerStatus()) ? "待缴费" : "已缴费");

    }

    return new PageUtils(page);
  }

  @Override
  public List<AppPowerEntity> findOrder(Object openid, Object status) {
    return appPowerDao.findInfoByStatus(String.valueOf(openid), String.valueOf(status));
  }

  @Override
  public void pay(Object orderId, Object payMoney) {

    String payTime = DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");

    appPowerDao.pay(String.valueOf(orderId), String.valueOf(payMoney), payTime, "2");

  }

}
